package alg.list;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author:yuziji
 * @create: 2023-04-26 08:50
 * 将链表反转并以数组形式返回
 */
public class NodeToArray {
    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        System.out.println(Arrays.toString(fastest(head)));
    }

    private static int[] nodeToArray(Node head){
        List<Integer> list = new ArrayList();
        while(head!=null){
            list.add(head.value);
            head = head.next;
        }

        int[] result = new int[list.size()];
        for (int i = list.size()-1; i >= 0; i--) {
            result[list.size()-i-1] = list.get(i);
        }
        return result;
    }

    static int count = 0;//表示数组长度
    static int index = 0;//表示数组下标
    private static int[] fastest(Node head){
        if (head==null){
            return new int[count];
        }
        count++;
        int[] result = fastest(head.next);
        result[index++] = head.value;
        return result;
    }
}
